package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.domian.ResponseResults;
import com.lh.domian.entity.Article;
import com.lh.domian.entity.Category;
import com.lh.domian.vo.CategoryVo;
import com.lh.mapper.CategoryMapper;
import com.lh.service.ArticleService;
import com.lh.service.CategoryService;
import com.lh.utils.BeanCopyUtils;
import com.lh.utils.SystemCodes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author LuoHang
 * @Date 2022-08-26 01:35
 **/
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private ArticleService articleService;

    public ResponseResults categoryList(){
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.select(Article.class,tableFieldInfo -> "id".equals(tableFieldInfo.getColumn()) ||
                "category_id".equals(tableFieldInfo.getColumn()));
        articleWrapper.eq(Article::getStatus,0);
        List<Article> articles = articleService.list(articleWrapper);
        //获取文章的分类ID，并且去重
        Set<Long> categoryIds = articles.stream()
                .map(Article::getCategoryId)
                .collect(Collectors.toSet());
        List<Category> categories = listByIds(categoryIds);
        //获取分类信息列表
        categories = categories.stream()
                .filter(category ->  SystemCodes.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //获取封装VO
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeans(categories, CategoryVo.class);
        return new ResponseResults(200,categoryVos);
    }
}
